﻿using AvaloniaApplicationDemo.Models;
using CommunityToolkit.Mvvm.ComponentModel;

namespace AvaloniaApplicationDemo.DTO;

/// <summary>
/// 从站信息数据传输对象，用于UI绑定和数据验证
/// 与 SlaveInfo 模型分离，避免UI直接修改业务模型
/// </summary>
public partial class SlaveInfoDto : ObservableObject
{
    /// <summary>
    /// 设备IP地址
    /// </summary>
    [ObservableProperty] private string _ipServer;

    /// <summary>
    /// 设备端口号
    /// </summary>
    [ObservableProperty] private int _portServer;

    /// <summary>
    /// TCP连接超时时间(毫秒)
    /// </summary>
    [ObservableProperty] private int _tcpTimeout;

    /// <summary>
    /// 从站ID (1-247)
    /// </summary>
    [ObservableProperty] private byte _slaveId;

    /// <summary>
    /// 起始寄存器地址
    /// </summary>
    [ObservableProperty] private ushort _startAddress;

    /// <summary>
    /// 读取寄存器数量 (1-30)
    /// </summary>
    [ObservableProperty] private ushort _quantity;

    /// <summary>
    /// 数据采集间隔时间(毫秒)
    /// </summary>
    [ObservableProperty] private int _intervalTime;

    /// <summary>
    /// 配置受否验证
    /// </summary>
    [ObservableProperty] private bool _isValid;

    /// <summary>
    /// 从站业务类
    /// </summary>
    [ObservableProperty] private SlaveInfo _slaveInfo;

    /// <summary>
    /// 从业务模型创建DTO实例
    /// </summary>
    /// <param name="slaveInfo">源业务模型</param>
    /// <returns>SlaveInfoDto实例</returns>
    public static SlaveInfoDto FromSlaveInfo(SlaveInfo slaveInfo)
    {
        return new SlaveInfoDto
        {
            IpServer = slaveInfo.IpServer,
            PortServer = slaveInfo.PortServer,
            TcpTimeout = slaveInfo.TcpTimeout,
            SlaveId = slaveInfo.SlaveId,
            StartAddress = slaveInfo.StartAddress,
            Quantity = slaveInfo.Quantity,
            IntervalTime = slaveInfo.IntervalTime ?? 2000
        };
    }

    /// <summary>
    /// 将DTO转换为业务模型对象
    /// </summary>
    /// <returns>SlaveInfo model模型实例</returns>
    public SlaveInfo ToSlaveInfo()
    {
        return new SlaveInfo
        {
            IpServer = IpServer,
            PortServer = PortServer,
            TcpTimeout = TcpTimeout,
            SlaveId = SlaveId,
            StartAddress = StartAddress,
            Quantity = Quantity,
            IntervalTime = IntervalTime
        };
    }

    /// <summary>
    /// 验证从站配置参数的有效性
    /// </summary>
    /// <param name="errorMessage">错误信息</param>
    /// <returns>验证是否通过</returns>
    public bool Validate(out string errorMessage)
    {
        if (string.IsNullOrWhiteSpace(IpServer))
        {
            errorMessage = "IP地址不能为空";
            return false;
        }

        if (SlaveId is <= 0 or > 247)
        {
            errorMessage = "从站ID必须在1-247范围内";
            return false;
        }

        if (Quantity is <= 0 or > 30)
        {
            errorMessage = "数据量必须在1-30范围内";
            return false;
        }

        if (IntervalTime <= 0)
        {
            errorMessage = "采集间隔时间必须大于0";
            return false;
        }

        errorMessage = string.Empty;
        return true;
    }
}